﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc。@Copy Right 2019
// 文件： CustomerCardRela.cs
// 项目名称：斑马车务
// 创建时间：2019/10/14
// 负责人：lsc
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using Dapper.Extensions;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Repository;
using YouWei.Utility;
using System.Text;

namespace YouWei.Infrastructure.Service
{

    /// <summary>
    ///
    /// </summary>
    public class UserCardRelaService
    {
        protected UserCardRelaRepository repository;

        public UserCardRelaService()
        {
            repository = new UserCardRelaRepository();
        }

        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(UserCardRela entity) { return repository.Insert(entity); }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<UserCardRela> entitys) { return repository.Insert(entitys); }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(UserCardRela entity) { return repository.Update(entity); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(UserCardRela entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public UserCardRela SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }

        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public UserCardRela Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<UserCardRela> QueryAll()
        {
            return repository.QueryAll();
        }

        #endregion

        /// <summary>
        /// 根据用户查询卡管理
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserCardRela GetByUser(int userId)
        {
            return repository.GetByUser(userId);
        }

        /// <summary>
        /// 获取卡级别
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int GetCarLevel(int userId)
        {
            return repository.GetCarLevel(userId);
        }

        /// <summary>
        /// 生成会员卡号  ACS+UserID+随机数
        /// 暂时没有规则和校验，测试之后修改
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string CreateCardCode(int userId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(userId);
            int left = 10 - sb.Length;
            if (left > 1)
            {
                Random ro = new Random(DateTime.Now.Second);
                for (int i = 0; i < left; i++)
                {
                    sb.Append(ro.Next(0, 9));
                }
            }
            return sb.ToString();
        }
    }

}